package com.qianniu.napi.admin.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.base.Strings;
import com.qianniu.napi.admin.entity.Goods;
import com.qianniu.napi.admin.entity.Rankdesc;
import com.qianniu.napi.admin.service.IRankdescService;
import com.qianniu.napi.common.util.ObjectUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.Date;

/**
 * <p>
 * <p>
 * </p>
 *
 * @author chenxuebin
 * @since 2018-07-13
 */
@Controller
public class RankdescController {

    private static final Logger logger = LoggerFactory.getLogger(RankdescController.class);

    @Autowired
    private IRankdescService rankdescService;

    @RequiresPermissions("admin:user:view")
    @RequestMapping("admin/rankdesc/index")
    public String index() {
        return "admin/rankdesc";
    }

    @RequiresPermissions("admin:user:view")
    @RequestMapping("/admin/rankdesc/list")
    @ResponseBody
    public Page<Rankdesc> alist(@RequestParam(value = "offset", required = false) Integer offset,
                                @RequestParam(value = "status", required = false) Integer status,
                                @RequestParam(value = "search", required = false) String search,
                                @RequestParam(value = "type", required = false) Integer type,
                                @RequestParam(value = "limit", required = false) Integer limit) {

        Wrapper<Rankdesc> wrapper = new EntityWrapper<Rankdesc>();
        int utype = ObjectUtil.getUserType();
        if (utype < 39)
            wrapper.eq("mpid", ObjectUtil.getMpid());
        else if (utype < 99)
            wrapper.eq("mid", ObjectUtil.getMid());

//        wrapper.eq("type", type);

//        if (status!=null&&status != 99) wrapper.eq("gtype", status);
//        if (!Strings.isNullOrEmpty(search))
//            wrapper.like("name", search).or().like("goods_no", search);

        wrapper.orderBy("id", false);
        Page<Rankdesc> page = rankdescService.selectPage(new Page<Rankdesc>(offset / limit+1, limit), wrapper);
        return page;
    }

    @RequestMapping("/admin/rankdesc/add")
    public String add(@Valid Rankdesc bean, BindingResult bindingResult) {
        // 字段校验
        if (bindingResult.hasErrors()) {
            logger.debug("There are errors! {}", bindingResult);
            return null;
        }
        bean.setCreatetime(new Date());
        bean.setMid(ObjectUtil.getMid());
        bean.setUid(ObjectUtil.getUid());
        bean.insert();
        return "admin/rankdesc";
    }

    @RequestMapping("/admin/rankdesc/getRankdescById")
    @ResponseBody
    public Rankdesc getRankdescById(long id) {
        Rankdesc rankdesc = rankdescService.selectById(id);
        return rankdesc;
    }

    @RequestMapping("/admin/rankdesc/deleteOne")
    @ResponseBody
    public int deleteOne(long id) {
        boolean isSucc = rankdescService.deleteById(id);
        return isSucc ? 1 : 0;
    }

    @RequestMapping("/admin/rankdesc/changeState")
    @ResponseBody
    public String changeState(long id, int state) {
        Rankdesc bean = new Rankdesc();
        bean.setId(id);

        rankdescService.updateById(bean);
        return "success";
    }

    @RequestMapping("/admin/rankdesc/modify")
    public String update(Rankdesc bean) {

        boolean isSuccc = rankdescService.updateById(bean);
        if (isSuccc) {
            return "redirect:index";
        }
        return "error";
    }

}
